﻿Imports Microsoft.ApplicationBlocks.Data
Imports System.Configuration
Imports System.Data.SqlClient
Imports System.Data
Imports DynamicDataEntryModel
Imports System.Data.Objects

Partial Class Admin_AssignQuestionsToHotDeal
    Inherits System.Web.UI.Page

    Protected Sub PageLoad(sender As Object, e As System.EventArgs) Handles Me.Load
        If Not Page.IsPostBack Then
            'Load the Hot Deal
            GetAllHostDeals()
            GetAllQuestions()
            'Load the questions for the first Hot Deal
            BindData()
        End If
    End Sub
    ''' <summary>
    ''' Get the questions for the Hot Deal
    ''' </summary>
    ''' <remarks></remarks>
    Private Sub BindData()
        'dgQuestions.DataSource = SqlHelper.ExecuteDataset(ConfigurationSettings.AppSettings("connectionString"), _
        '                    CommandType.StoredProcedure, "dq_GetDynamicQuestionsForHotDeal", _
        '                    New SqlParameter("@HotDealID", hotDeal.SelectedItem.Value)).Tables(0)

        Dim objHotDeal As New App_Code.HotDeal()
        Dim dt = objHotDeal.GetDynamicQuestionsForHotDeal(CType(hotDeal.SelectedItem.Value, Integer))

        dgQuestions.DataSource = dt
        dgQuestions.DataBind()

        dgQuestions.Visible = dgQuestions.Items.Count > 0
        noQuestionsForCustomerTypeMsg.Visible = Not dgQuestions.Visible
    End Sub

    Private Sub HotDealSelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles hotDeal.SelectedIndexChanged
        BindData()
    End Sub

    ''' <summary>
    ''' When Users click Add Question button
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Private Sub BtnAddQuestionClick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAddQuestion.Click
        'Add the selected question to the selected customer's dynamic question queue
        'SqlHelper.ExecuteNonQuery(ConfigurationSettings.AppSettings("connectionString"), _
        '                    CommandType.StoredProcedure, "dq_AddDynamicQuestion", _
        '                    New SqlParameter("@HotDealID", hotDeal.SelectedItem.Value), _
        '                    New SqlParameter("@QuestionID", question.SelectedItem.Value))

        Dim objHotDeal As New App_Code.HotDeal()
        objHotDeal.AddDynamicQuestion(CType(hotDeal.SelectedItem.Value, Integer), CType(question.SelectedItem.Value, Integer))

        BindData()
    End Sub
    Private Sub DgQuestionsDeleteCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs) Handles dgQuestions.DeleteCommand
        'Delete the selected Dynamic Question
        Dim id As Integer = Convert.ToInt32(dgQuestions.DataKeys(e.Item.ItemIndex))

        SqlHelper.ExecuteNonQuery(ConfigurationSettings.AppSettings("connectionString"), _
                            CommandType.StoredProcedure, "dq_DeleteDynamicQuestion", _
                            New SqlParameter("@DynamicQuestionID", id))

        BindData()
    End Sub

    Private Sub DgQuestionsItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DataGridItemEventArgs) Handles dgQuestions.ItemDataBound
        If e.Item.ItemType = ListItemType.Item OrElse e.Item.ItemType = ListItemType.AlternatingItem Then
            'Add a confirm button to the Remove button
            Dim lb As LinkButton = CType(e.Item.Cells(0).Controls(0), LinkButton)
            lb.Attributes("onclick") = "javascript:return confirm('Are you sure you want to remove this question?');"

            'Enable/Disable MoveUp/MoveDown buttons as needed
            Dim up As ImageButton = CType(e.Item.FindControl("btnMoveUp"), ImageButton)
            Dim down As ImageButton = CType(e.Item.FindControl("btnMoveDown"), ImageButton)

            up.Visible = e.Item.ItemIndex <> 0
            down.Visible = e.Item.ItemIndex <> dgQuestions.Columns.Count - 1
        End If
    End Sub

    Private Sub DgQuestionsItemCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs) Handles dgQuestions.ItemCommand
        If e.CommandName = "MoveUp" Then
            'Swap the current item's SortOrder with the SortOrder of the item with the immediately smaller SortOrder
            'Delete the selected Dynamic Question
            Dim id As Integer = Convert.ToInt32(dgQuestions.DataKeys(e.Item.ItemIndex))

            SqlHelper.ExecuteNonQuery(ConfigurationSettings.AppSettings("connectionString"), _
                                            CommandType.StoredProcedure, "dq_MoveUpSortOrder", _
                                            New SqlParameter("@DynamicQuestionID", id))

            BindData()
        ElseIf e.CommandName = "MoveDown" Then
            'Swap the current item's SortOrder with the SortOrder of the item with the immediately greater SortOrder
            'Delete the selected Dynamic Question
            Dim id As Integer = Convert.ToInt32(dgQuestions.DataKeys(e.Item.ItemIndex))

            SqlHelper.ExecuteNonQuery(ConfigurationSettings.AppSettings("connectionString"), _
                                            CommandType.StoredProcedure, "dq_MoveDownSortOrder", _
                                            New SqlParameter("@DynamicQuestionID", id))

            BindData()
        End If
    End Sub
    Private Sub GetAllHostDeals()
        Dim objHotDeal As New App_Code.HotDeal()
        hotDeal.DataSource = objHotDeal.GetAllHotDeals()
        'hotDeal.DataSource = lsthotDeals
        hotDeal.DataBind()
    End Sub
    Private Sub GetAllQuestions()
        Dim objHotDeal As New App_Code.HotDeal()
        Dim questions = objHotDeal.GetAllQuestions()
        question.DataSource = questions
        question.DataBind()
    End Sub
End Class
